package org.eclipse.emf.cdo.ui.merge;

import java.util.Map;
import java.util.Set;

import org.eclipse.ui.part.*;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.commit.CDOChangeSetData;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.impl.CDOResourceImpl;
import org.eclipse.emf.cdo.internal.ui.dialogs.BranchSelectionDialog;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.spi.cdo.DefaultCDOMerger;
import org.eclipse.emf.spi.cdo.DefaultCDOMerger.ChangedInSourceAndTargetConflict;
import org.eclipse.emf.spi.cdo.DefaultCDOMerger.Conflict;
import org.eclipse.emf.spi.cdo.DefaultCDOMerger.ConflictException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.net4j.util.container.IPluginContainer;

/**
 * Premature test class, used to call the functions without an user action.
 * Do not use any longer !
 * 
 * @author Thomas Halmetschlager
 *
 * @param <a>
 */
@Deprecated
public class CreateMergeTestContent<a> {
	
	
	private CDOChangeSetData result;
	private Map<CDOID, Conflict> conflicts;
	private CDOSession session; 
	private CDOBranch sourceBranch;
	private CDOBranch targetBranch;
	
	public CreateMergeTestContent() {
		session = (CDOSession)IPluginContainer.INSTANCE.getElement("org.eclipse.emf.cdo.sessions", "cdo",
        "tcp://localhost:2036?repositoryName=repo1&automaticPackageRegistry=true");
		CreateTestData(session);
		
	}
	public CreateMergeTestContent(CDOSession session) {
		CreateTestData(session);
	}
	 
	private void CreateTestData(CDOSession session){
		CDOBranch mainBranch = session.getBranchManager().getMainBranch();
	    CDOTransaction transaction = session.openTransaction(mainBranch);
	    
	    CDOBranchPoint targetBranchPoint;
	    
	   /* BranchSelectionDialog dialog = new BranchSelectionDialog(getSite().getPage(), transaction);
	    if (dialog.open() == Dialog.OK)
	    {
	      targetBranchPoint = dialog.getTargetBranchPoint();
	      if (targetBranchPoint == null)
	      {
	        
	      }
	    }
	    else
	    {
	     
	    }*/

	    
	    //CDOResource resource = transaction.createResource("/testres");
	  //  EList<EObject> contents = resource.getContents();
	    //add 6
	    //long time1 = transaction.commit().getTimeStamp();
	    
	    //CDOBranch source1 = mainBranch.getBranch("mergetest1");
	    //CDOBranch source2 = mainBranch.getBranch("mergetest2");
	    //CDOBranch source3 = mainBranch.getBranch("mergetest3");
	    //CDOBranch source4 = mainBranch.getBranch("mergetest4");
	    //CDOBranch source5 = mainBranch.getBranch("mergetest5");
	    //CDOBranch source6 = mainBranch.getBranch("mergetest6");
	    sourceBranch = mainBranch.getBranch("mergetest12");
		  //  CDOTransaction trans1 = session.openTransaction(source2);
	   // trans1.merge(source1.getHead(), new DefaultCDOMerger.PerFeature.ManyValued());
	    /*	    CDOTransaction transaction2 = session.openTransaction(source7);
	    ResourceSet a = transaction.getResourceSet();
	    
	    for( Resource b : a.getResources()){
	    	CDOResourceImpl xy = (CDOResourceImpl) b;
	    		    	for(EObject z: xy.getContents()){
	    		CDOResourceImpl f =  (CDOResourceImpl) z;
	    		int ac;
	    		ac=0;
	    	}
	    	
	    	
	    }
	    
		   transaction2.close();*/
	    result = null;
	    targetBranch = transaction.getBranch();
	    try{
	    result = transaction.merge(sourceBranch.getHead(), new DefaultCDOMerger.PerFeature.ManyValued());
	    }
	    catch (ConflictException e) {
			// TODO: handle exception
	    	System.out.println(e.getMessage());
	    	result = e.getMerger().getResult();
	    	
	    	conflicts = e.getMerger().getConflicts();
	    	
	    	for(CDOID conflictID :conflicts.keySet()){
	    		Conflict conflict = conflicts.get(conflictID);
	    		
	    		System.out.println(conflict.toString());
	    	}
		}
	   
	    
	    //transaction.commit();
	    transaction.close();
	   // trans1.close();
	   // transaction.commit();

	  //sleep
	 /*   CDOTransaction tx2 = session.openTransaction(source2);
	    CDOResource res2 = tx2.getResource("/res");
	    EList<EObject> contents2 = res2.getContents();
	  //add 1 
	    tx2.commit();
	    tx2.close();

	    transaction.merge(source2.getHead(), new DefaultCDOMerger.PerFeature.ManyValued());
	    transaction.commit();*/

	   // CDOChangeSetData result = transaction.merge(source2.getHead(), new DefaultCDOMerger.PerFeature.ManyValued());
	    
	}
	public CDOBranch getSourceBranch() {
		return sourceBranch;
	}
	public CDOBranch getTargetBranch() {
		return targetBranch;
	}
	public CDOChangeSetData getResult() {
		return result;
	}
	public void setResult(CDOChangeSetData result) {
		this.result = result;
	}
	public Map<CDOID, Conflict> getConflicts() {
		return conflicts;
	}
	public void setConflicts(Map<CDOID, Conflict> conflicts) {
		this.conflicts = conflicts;
	}
	public CDOSession getSession() {
		return session;
	}
	public void setSession(CDOSession session) {
		this.session = session;
	}
}
